package com.ruoyi.crm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.domain.CrmOrderTrace;
import com.ruoyi.crm.service.ICrmOrderTraceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商机跟进记录Controller
 *
 * @author yanwenliang
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/crm/trace")
public class CrmOrderTraceController extends BaseController
{
    @Autowired
    private ICrmOrderTraceService crmOrderTraceService;

    /**
     * 查询商机跟进记录列表
     */
//    @PreAuthorize("@ss.hasPermi('crm:trace:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmOrderTrace crmOrderTrace)
    {
        startPage();
        List<CrmOrderTrace> list = crmOrderTraceService.selectCrmOrderTraceList(crmOrderTrace);
        return getDataTable(list);
    }

    /**
     * 导出商机跟进记录列表
     */
//    @PreAuthorize("@ss.hasPermi('crm:trace:export')")
    @Log(title = "商机跟进记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmOrderTrace crmOrderTrace)
    {
        List<CrmOrderTrace> list = crmOrderTraceService.selectCrmOrderTraceList(crmOrderTrace);
        ExcelUtil<CrmOrderTrace> util = new ExcelUtil<CrmOrderTrace>(CrmOrderTrace.class);
        util.exportExcel(response, list, "商机跟进记录数据");
    }

    /**
     * 获取商机跟进记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('crm:trace:query')")
    @GetMapping(value = "/{traceOrderId}")
    public AjaxResult getInfo(@PathVariable("traceOrderId") String traceOrderId)
    {
        return success(crmOrderTraceService.selectCrmOrderTraceByTraceOrderId(traceOrderId));
    }

    /**
     * 新增商机跟进记录
     */
//    @PreAuthorize("@ss.hasPermi('crm:trace:add')")
    @Log(title = "商机跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmOrderTrace crmOrderTrace)
    {
        return toAjax(crmOrderTraceService.insertCrmOrderTrace(crmOrderTrace));
    }

    /**
     * 修改商机跟进记录
     */
//    @PreAuthorize("@ss.hasPermi('crm:trace:edit')")
    @Log(title = "商机跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmOrderTrace crmOrderTrace)
    {
        return toAjax(crmOrderTraceService.updateCrmOrderTrace(crmOrderTrace));
    }

    /**
     * 删除商机跟进记录
     */
//    @PreAuthorize("@ss.hasPermi('crm:trace:remove')")
    @Log(title = "商机跟进记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{traceOrderIds}")
    public AjaxResult remove(@PathVariable String[] traceOrderIds)
    {
        return toAjax(crmOrderTraceService.deleteCrmOrderTraceByTraceOrderIds(traceOrderIds));
    }

    /**
     * 获取数据
     * @param crmOrderTrace
     * @return
     */
    @PostMapping("/getOrderTraceListNotPage")
    public AjaxResult getOrderTraceListNotPage(@RequestBody CrmOrderTrace crmOrderTrace){
        return AjaxResult.success(crmOrderTraceService.list(new LambdaQueryWrapper<CrmOrderTrace>()
                .eq(CrmOrderTrace::getOrderId, crmOrderTrace.getOrderId())
                .orderByDesc(CrmOrderTrace::getTraceOrderTime)));
    }
}
